<template>
  <div class="cart-page">
    <div class="container">
      <el-breadcrumb separator="/" class="breadcrumb">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>购物车</el-breadcrumb-item>
      </el-breadcrumb>
    <el-card class="card" v-if="$store.getters['cart/validList'].length > 0">
      <div class="cart">
        <table>
          <thead>
            <tr>
              <th width="120"><el-checkbox v-model="isCheckAll">全选</el-checkbox></th>
              <th width="400">商品信息</th>
              <th width="220">单价</th>
              <th width="180">数量</th>
              <th width="180">小计</th>
              <th width="140">操作</th>
            </tr>
          </thead>
          <!-- 有效商品 -->
          <tbody>
            <tr v-for="item in $store.getters['cart/validList']" :key="item.skuId">
              <td><el-checkbox @change="checkOne(item)" v-model="item.selected"></el-checkbox></td>
              <td>
                <div class="goods">
                  <RouterLink :to="`/product/${item.id}`"><img :src="item.picture" alt=""></RouterLink>
                  <div>
                    <p class="name ellipsis">{{item.name}}</p>
                    <!-- 选择规格组件 -->
                    <CartSku @change="updateCartSku(item.skuId, $event)" :attrsText="item.attrsText" :skuId="item.skuId"></CartSku>
                    <!-- <p class="attr">{{item.attrsText}}</p> -->
                  </div>
                </div>
              </td>
              <td class="tc">
                <p>&yen;{{item.nowPrice}}</p>
                <!-- <p>比加入时降价 <span class="red">&yen;20.00</span></p> -->
              </td>
              <td class="tc">
                <el-input-number v-model="item.count" size="small" :min="1" :max="item.stock"></el-input-number>
              </td>
              <td class="tc"><p class="f16 red">&yen;{{item.nowPrice*100*item.count/100}}</p></td>
              <td class="tc">
                <p><a href="javascript:;">移入收藏夹</a></p>
                <p><a @click="deleteCart(item.skuId)" class="green" href="javascript:;">删除</a></p>
                <p><a href="javascript:;">找相似</a></p>
              </td>
            </tr>
          </tbody>
          <!-- 无效商品 -->
          <!-- <tbody v-if="$store.getters['cart/invalidList'].length>0">
            <tr><td colspan="6"><h3 class="tit">失效商品</h3></td></tr>
            <tr v-for="item in $store.getters['cart/validList']" :key="item.skuId">
              <td><el-checkbox style="color:#eee;"></el-checkbox></td>
              <td>
                <div class="goods">
                  <RouterLink :to="`/product/${item.id}`"><img :src="item.picture" alt=""></RouterLink>
                  <div>
                    <p class="name ellipsis">{{item.name}}</p>
                    <p class="attr">{{item.attrsText}}</p>
                  </div>
                </div>
              </td>
              <td class="tc"><p>&yen;{{item.nowPrice}}</p></td>
              <td class="tc">{{item.count}}</td>
              <td class="tc">
                <p><a @click="deleteCart(item.skuId)" class="green" href="javascript:;">删除</a></p>
                <p><a href="javascript:;">找相似</a></p>
              </td>
            </tr>
          </tbody> -->
        </table>
      </div>
        <!-- 操作栏 -->
        <div class="action">
            <div class="batch">
              <el-checkbox v-model="isCheckAll">全选</el-checkbox>
              <a href="javascript:;" @click="batchDeleteCart">删除商品</a>
              <a href="javascript:;">移入收藏夹</a>
              <!-- <a href="javascript:;">清空失效商品</a> -->
            </div>
            <div class="total">
              共 {{$store.getters['cart/validTotal']}} 件商品，已选择 {{$store.getters['cart/selectedTotal']}} 件，商品合计：
              <span class="red">¥{{$store.getters['cart/selectedTotalCount']}}</span>
              <el-button type="primary" @click="checkout">下单结算</el-button>
            </div>
        </div>
      </el-card>
      <el-card v-else  class="card">
        <el-empty :image-size="100">快去挑选商品吧！</el-empty>
      </el-card>
      <!-- 猜你喜欢 -->
      <el-card>
        <GoodRelevant />
    </el-card>
    </div>
  </div>
</template>
<script>
import GoodRelevant from '@/views/goods/components/goods-relevant'
import { useStore } from 'vuex'
import { computed } from 'vue'
import { ElMessageBox, ElMessage } from 'element-plus'
import CartSku from './components/cart-sku'
import { useRouter } from 'vue-router'
export default {
  name: 'CartPage',
  components: { GoodRelevant, CartSku },
  setup () {
    const store = useStore()
    const checkOne = (item) => {
      // console.log(item)
      store.commit('cart/updateCart', { skuId: item.skuId, selected: item.selected })
    }
    // const checkAll = (selected) => {
    //   // selected = !selected
    //   store.dispatch('cart/checkAll', selected)
    // }
    const isCheckAll = computed({
      get: () => store.getters['cart/isSelectedAll'],
      set: (selected) => store.dispatch('cart/checkAll', selected)
    })
    const deleteCart = (skuId) => {
      ElMessageBox.confirm(
        '确定要删除这件商品吗？',
        '温馨提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(() => {
          store.dispatch('cart/deleteCart', skuId).then(() => {
            ElMessage({
              type: 'success',
              message: '删除成功'
            })
          })
        })
        .catch(() => {
          ElMessage({
            type: 'info',
            message: '取消删除'
          })
        })
    }
    const batchDeleteCart = () => {
      ElMessageBox.confirm(
        '确定要删除选中的商品吗？',
        '温馨提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(() => {
          store.dispatch('cart/batchDeleteCart').then(() => {
            ElMessage({
              type: 'success',
              message: '删除成功'
            })
          })
        })
        .catch(() => {
          ElMessage({
            type: 'info',
            message: '取消删除'
          })
        })
    }
    // 修改规格
    const updateCartSku = (oldSkuId, newSku) => {
      // console.log(newSku)
      store.dispatch('cart/updateCartSku', { oldSkuId, newSku })
    }
    // 结算
    const router = useRouter()
    const checkout = () => {
      // 判断是否选中商品
      // 判断是否登录
      // 没登录 路由拦截到登录页面
      if (store.getters['cart/selectedList'].length === 0) {
        ElMessage({
          type: 'warning',
          message: '至少选择一件商品'
        })
        return
      }
      // 没登录
      if (!store.state.user.profile.token) {
        // 没有token
        ElMessageBox.confirm(
          '该功能需要登录才能使用！立即登录？',
          '温馨提示',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }
        ).then(() => {
          router.push('/member/checkout')
        }).catch(() => {})
      } else {
        router.push('/member/checkout')
      }
    }
    return { isCheckAll, checkOne, deleteCart, batchDeleteCart, updateCartSku, checkout }
  }
}
</script>
<style scoped lang="less">
@import '../../assets/styles/variable.less';
.tc {
  text-align: center;
  .xtx-numbox {
    margin: 0 auto;
    width: 120px;
  }
}
.red {
  color: @priceColor;
}
.green {
  color: @c;
}
.f16 {
  font-size: 16px;
}
.goods {
  display: flex;
  align-items: center;
  img {
    width: 100px;
    height: 100px;
  }
  > div {
    width: 280px;
    font-size: 16px;
    padding-left: 10px;
    .attr {
      font-size: 14px;
      color: #999;
    }
  }
}
.action {
  display: flex;
  background: #fff;
  margin-top: 20px;
  height: 80px;
  align-items: center;
  font-size: 16px;
  justify-content: space-between;
  padding: 0 30px;
  .batch {
    a {
      margin-left: 20px;
    }
  }
  .red {
    font-size: 18px;
    margin-right: 20px;
    font-weight: bold;
  }
}
.tit {
  color: #666;
  font-size: 16px;
  font-weight: normal;
  line-height: 50px;
}
cart-page {
  .cart {
    background: #fff;
    color: #666;
    table {
      border-spacing: 0;
      border-collapse: collapse;
      line-height: 24px;
      th,td{
        padding: 10px;
        border-bottom: 1px solid #f5f5f5;
        &:first-child {
          text-align: left;
          padding-left: 30px;
          color: #999;
        }
      }
      th {
        font-size: 16px;
        font-weight: normal;
        line-height: 50px;
      }
    }
  }
}
.breadcrumb{
  margin: 30px;
}
:deep(.card){
  margin-bottom: 20px;
  overflow: visible ;
}
</style>
